#include "mainwindow.h"
#include "appsettings.h"
#include "qlib/librarydock.h"
#include "qlib/librarymodel.h"
#include "qlib/componentwindow.h"

#include <QtGui/qaction.h>
#include <QtGui/qapplication.h>
#include <QtGui/qevent.h>
#include <QtGui/qmdiarea.h>
#include <QtGui/qmdisubwindow.h>
#include <QtGui/qmenubar.h>
#include <QtGui/qmessagebox.h>
#include <QtGui/qtabbar.h>

const char *MainWindow::m_languages[] =
{
    QT_TRANSLATE_NOOP("MainWindow", "English"),    "en",
    QT_TRANSLATE_NOOP("MainWindow", "Russian"),    "ru"
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("QEDA"));
    setWindowIcon(QIcon(":/images/core.png"));
    restoreGeometry(g_appSettings->asByteArray("Geometry"));
    m_mdiArea = new QMdiArea(this);
    m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_mdiArea->setViewMode(QMdiArea::TabbedView);
    m_tabBar = m_mdiArea->findChild<QTabBar*>();
    Q_ASSERT(m_tabBar);
    m_tabBar->setExpanding(false);
    m_tabBar->setTabsClosable(true);
    connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
            this, SLOT(closeTab(int)));

    setCentralWidget(m_mdiArea);
    m_libModel = new LibraryModel(this);
    createActions();
    createMenus();
    createDocks();
}

MainWindow::~MainWindow()
{
    
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    g_appSettings->set("Geometry", saveGeometry());
    g_appSettings->set("LibDockWidth", m_libDock->width());
    g_appSettings->writeAll();

    bool isOk = true;
    while (!m_mdiArea->subWindowList().isEmpty() && isOk)
    {
        QMdiSubWindow *subWin = m_mdiArea->subWindowList().first();
        QWidget *w = subWin->widget();
        isOk = w->close();
        if (isOk)
        {
            m_mdiArea->setActiveSubWindow(subWin);
            m_mdiArea->closeActiveSubWindow();
        }
    }
    if (!isOk)
        event->ignore();
    else
        event->accept();
}

void MainWindow::createActions()
{
    QAction *act;

    // FILE
    act = new QAction(QIcon(":/images/file_open.png"), tr("&Open..."), this);
    act->setShortcuts(QKeySequence::Open);
    connect(act, SIGNAL(triggered()), this, SLOT(fileOpen()));
    m_actions["File/Open"] = act;

    act = new QAction(QIcon(":/images/file_save.png"), tr("&Save"), this);
    act->setShortcuts(QKeySequence::Save);
    connect(act, SIGNAL(triggered()), this, SLOT(fileSave()));
    m_actions["File/Save"] = act;

    act = new QAction(tr("E&xit"), this);
    act->setShortcuts(QKeySequence::Quit);
    connect(act, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
    m_actions["File/Exit"] = act;

    // VIEW
    QActionGroup *langGroup = new QActionGroup(this);
    const int langCount = (sizeof(m_languages) / sizeof(char*)) / 2;
    const QString currLang = g_appSettings->asString("View/Language");
    for (int i = 0; i < langCount; ++i)
    {
        act = new QAction(tr(m_languages[2*i]), this);
        act->setData(m_languages[2*i + 1]);
        act->setCheckable(true);
        act->setChecked(currLang.compare(m_languages[2*i + 1]) == 0);
        connect(act, SIGNAL(triggered()), this, SLOT(viewLanguage()));
        langGroup->addAction(act);
        m_actions["View/Language/"+QString::number(i)] = act;
    }

    // HELP
    act = new QAction(tr("&About..."), this);
    connect(act, SIGNAL(triggered()), this, SLOT(helpAbout()));
    m_actions["Help/About"] = act;
}

void MainWindow::createDocks()
{
    setTabPosition(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea, QTabWidget::West);
    m_libDock = new LibraryDock(m_libModel, this);
    addDockWidget(Qt::LeftDockWidgetArea, m_libDock);
    m_libDock->setWidth(g_appSettings->asUint("LibDockWidth", 200));
    connect(m_libDock, SIGNAL(componentEditRequested(QedaComponent*)),
            this, SLOT(componentEdit(QedaComponent*)));
}

void MainWindow::createMenus()
{
    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(m_actions["File/Open"]);
    fileMenu->addAction(m_actions["File/Save"]);
    fileMenu->addSeparator();
    fileMenu->addAction(m_actions["File/Exit"]);

    QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
    QMenu *langMenu = viewMenu->addMenu(tr("Choose language"));
    const int langCount = (sizeof(m_languages) / sizeof(char*)) / 2;
    for (int i = 0; i < langCount; ++i)
        langMenu->addAction(m_actions["View/Language/" + QString::number(i)]);

    QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(m_actions["Help/About"]);
}

QMdiSubWindow *MainWindow::findComponentWin(QedaComponent *component)
{
    QMdiSubWindow *subWin = 0;
    for (int i = 0; i < m_mdiArea->subWindowList().count(); ++i)
    {
        subWin = m_mdiArea->subWindowList().at(i);
        ComponentWindow *cw = qobject_cast<ComponentWindow*>(subWin->widget());
        if (cw && cw->component() == component)
            return subWin;
    }
    return 0;
}
